{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# dict 测试"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过“键值对”（key-value）访问："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ee\n"
     ]
    }
   ],
   "source": [
    "dict = {1: 1, 2: 'aa', 'D': 'ee', 'Ty': 45}\n",
    "print(dict['D'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "dict.get(key,[default]) ：default为可选项，用于指定当‘键’不存在时 返回一个默认值，如果省略，默认返回None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aa\n",
      "None\n",
      "['字典中不存在键为4的元素']\n"
     ]
    }
   ],
   "source": [
    "dict = {1: 1, 2: 'aa', 'D': 'ee', 'Ty': 45}\n",
    "print(dict.get(2))\n",
    "print(dict.get(3))\n",
    "print(dict.get(4, ['字典中不存在键为4的元素']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "key 转list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 'D', 'Ty']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(dict.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
